<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Product categories
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Product_categories_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($q = '')
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($q != '')
		{
			$this->db->like('name_vn', $q);
			$this->db->or_like('keyword', str_replace(' ', '-', $q));
			$this->db->select('id, parent');
			$query = $this->db->get('product_categories');
			foreach ($query->result_array() as $row)
			{
				$ids[] = $row['parent'];
				$ids[] = $row['id'];
			}
			$ids = $this->search_cat($ids);
			$this->db->where_in('c.id', $ids);
		}
		$last_login = $GLOBALS['user']['last_login'];
		$this->db->from('product_categories AS c');
		$this->db->select('c.*, COUNT(s.parent) AS chirld, IF ('.sprintf('c.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
		$this->db->group_by('id');
		$this->db->select('(SELECT COUNT(id) FROM products WHERE (cat LIKE CONCAT("%\"", c.id, "\"%")) OR cat = c.id) AS product', false);
		$this->db->join('product_categories as s', 'c.id = s.parent', 'left');
		$this->db->order_by('c.sort_order asc');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	private function search_cat($ids)
	{
		$break = false;
		$this->db->where_in('id', $ids);
		$this->db->select('parent');
		$query = $this->db->get('product_categories');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$ids[] = $row['parent'];
				if ($row['parent'] == 0) $break = true;
			}
			if (!$break) $ids = $this->search_cat($ids);
		}
		return $ids;
	}

	public function info_cat()
	{
		$info_cat = array(
			'num_cat' => $this->db->select('id')->get('product_categories')->num_rows(),
			'num_deactive' => $this->db->select('id')->where('active', 0)->get('product_categories')->num_rows(),
			'num_deleted' => $this->db->select('id')->where('deleted', 1)->get('product_categories')->num_rows()
		);
		return $info_cat;
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('product_categories');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('product_categories');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function process($data, $id = '')
	{
		if(!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');	
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('product_categories', $data);
			write_log('update_product_categories', $data['name_vn']);
		}
		else
		{
			$this->db->insert('product_categories', $data);
			write_log('add_product_categories', $data['name_vn']);
		}
		return true;
	}
}

/* End of file product_categories_model.php */
/* Location: ./application/models/product_categories_model.php */